package com.example.chessmigame;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddActivity extends AppCompatActivity{
    private EditText title;   //标题
    private EditText context;   //内容
    private String get_title;
    private String get_context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        title = (EditText) findViewById(R.id.title_add);
        context = (EditText) findViewById(R.id.context_add);
    }

    public void finishadd(View v){
        NoteManager noteManager = new NoteManager(AddActivity.this);
        get_title = title.getText().toString().trim();
        get_context = context.getText().toString().trim();

        if (TextUtils.isEmpty(get_title) || TextUtils.isEmpty(get_context)) {
            Toast.makeText(AddActivity.this, "添加信息不能为空", Toast.LENGTH_SHORT).show();
        } else {
            Note note = new Note();
            note.title = get_title;
            note.context = get_context;
            boolean add = noteManager.insert(note);
            //如果添加数据成功，跳到待办事项界面，并通过传值，让目标界面进行刷新
            if (add) {
                Intent intent = new Intent();
                intent.setClass(AddActivity.this, ListActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra("Insert", 1);
                startActivity(intent);
            } else {
                Toast.makeText(AddActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
            }
        }

    }

    public void addtolist(View view) {
            Intent intent = new Intent(AddActivity.this, ListActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
    }
}